package net.hn.hnms.biz.plan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.plan.domain.DimShift;
import net.hn.hnms.biz.plan.mapper.DimShiftMapper;
import net.hn.hnms.biz.plan.request.DimShiftReq;
import net.hn.hnms.biz.plan.service.IDimShiftService;
import net.hn.hnms.biz.plan.vo.DimShiftVO;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhaoyang
 * 班次信息管理服务层实现
 * @date 2022-08-01
 */

@Service
@DS("slave")
public class DimShiftServiceImpl extends BaseService<DimShift, DimShiftVO> implements IDimShiftService {

    @Autowired
    private DimShiftMapper dimShiftMapper;

    /**
     * @param dimShift
     * @param pageQuery       查询班次信息列表
     */
    @Override
    public ResponsePage<DimShiftVO> selectPageList(DimShiftReq dimShift, PageQuery pageQuery) {

        Page<DimShift> page = dimShiftMapper.selectPage(pageQuery.build(), buildQuery(dimShift).orderByAsc(DimShift::getShiftStartTime));
        List<DimShiftVO> list =page.getRecords().stream().map(o ->convert(o, DimShiftVO.class)).collect(Collectors.toList());
        return new ResponsePage<>(list, page.getTotal());
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<DimShift> 查询条件
     */
    private LambdaQueryWrapper<DimShift> buildQuery(DimShiftReq req) {
        return new LambdaQueryWrapper<DimShift>()
                .like(StrUtil.isNotBlank(req.getShiftName()), DimShift::getShiftName, req.getShiftName())
                .apply(req.getBeginTime() != null,
                        "time_ge( shift_start_time ,'" + DateUtil.format(req.getBeginTime(), DatePattern.NORM_TIME_FORMAT) + "')")
                .apply(req.getEndTime() != null,
                        "time_le( shift_end_time ,'" + DateUtil.format(req.getEndTime(), DatePattern.NORM_TIME_FORMAT) + "')")
                .eq(StrUtil.isNotBlank(req.getMineCode()), DimShift::getMineCode, req.getMineCode())
                .eq(StrUtil.isNotBlank(req.getStatus()), DimShift::getStatus, req.getStatus())
                ;
    }

    /**
     *
     * @param dimShift
     * 查询班次信息列表
     */
    @Override
    public List<DimShiftVO> selectList(DimShiftReq dimShift) {
        List<DimShift> list = dimShiftMapper.selectList(buildQuery(dimShift));
        return list.stream().map(o -> convert(o, DimShiftVO.class)).collect(Collectors.toList());
    }

    /**
     * @param dimShift 新增班次信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DimShiftReq dimShift) {
//        QueryWrapper<DimShift> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("drivage_face_code", dimShift.get());
//        if (dimShiftMapper.selectCount(queryWrapper) > 0) {
//            throw new HuaNengException(Constants.FAIL, "已存在相同的计划编号");
//        }
        DimShift insertObj = new DimShift();
        BeanUtil.copyProperties(dimShift, insertObj);
        insertObj.setStatus("0");

        return dimShiftMapper.insert(insertObj);
    }

    /**
     * @param idList 批量删除 id集合
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(List<String> idList) {
        dimShiftMapper.deleteBatchIds(idList);
    }

    /**
     * @param dimShift 单个修改 按照主键修改信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(DimShiftReq dimShift) {
        if (StrUtil.isBlank(dimShift.getShiftId())) {
            throw new HuaNengException(Constants.FAIL, "id不能为空");
        }
        DimShift updateObj = new DimShift();
        BeanUtil.copyProperties(dimShift, updateObj);
        int updateCount = dimShiftMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    /**
     * @param id 按主键id查询详情
     */
    @Override
    public DimShiftVO selectDetailById(String id) {
        DimShift dimShift = dimShiftMapper.selectById(id);
        if (dimShift == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return convertWithUser(dimShift, DimShiftVO.class);
    }


    /**
     * 启用/禁用
     * @param id 数据id
     * @param status  status: 0:启用 1:禁用
     */
    public void changeStatus(String id, Integer status) {
        if (status == 1 || status == 0) {
            DimShift planMining = dimShiftMapper.selectById(id);
            if (planMining == null) {
                throw new HuaNengException(Constants.FAIL, "数据不存在");
            }
            planMining.setStatus(status + "");
            int res = dimShiftMapper.updateById(planMining);
            if (res == 0) {
                throw new HuaNengException(Constants.FAIL, "数据不存在");
            }

            return;
        }
        throw new HuaNengException(Constants.FAIL, "传入的参数错误");
    }
}
